package cn.com.bluemoon.daps.model.service.impl;

import cn.com.bluemoon.daps.api.sys.RemoteSystemDatasourceService;
import cn.com.bluemoon.daps.api.sys.RemoteSystemInfoService;
import cn.com.bluemoon.daps.api.sys.RemoteSystemUserService;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.exception.DapException;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.system.dto.SystemUserDto;
import cn.com.bluemoon.daps.system.entity.DapSystemDatasource;
import cn.com.bluemoon.daps.system.entity.DapSystemInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 调用系统模块接口的门面
 */
@Service
@Slf4j
public class DapSystemModuleFacade {
    @Autowired
    private RemoteSystemDatasourceService remoteSystemDatasourceService;

    @Autowired
    RemoteSystemInfoService remoteSystemInfoService;

    @Autowired
    private RemoteSystemUserService remoteSystemUserService;


    /**
     * 获取数据源
     *
     * @param dataSourceId 数据源id
     * @return
     */
    public DapSystemDatasource getDbTypeByDataSourceId(String dataSourceId) {
        Optional<DapSystemDatasource> datasourceOpt = Optional.ofNullable(remoteSystemDatasourceService.getById(dataSourceId));
        DapSystemDatasource datasource = datasourceOpt.orElseThrow(() -> new DapException("获取系统数据源失败！！"));
        return datasource;
    }

    /**
     * 获取系统信息
     * @param systemId 系统id
     * @return 获取系统信息
     */
    public Optional<DapSystemInfo> getSystemInfo(String systemId){
        ResultBean<DapSystemInfo> systemInfoR = remoteSystemInfoService.getSystemInfo(systemId);
        DapSystemInfo systemInfo = systemInfoR.getContent();
        return Optional.ofNullable(systemInfo);
    }

    /**
     * id获取数据源
     * @param dataSourceId id
     * @return id获取数据源
     */
    public Optional<DapSystemDatasource> getSysDataSourceById(String dataSourceId){
        ResultBean<DapSystemDatasource> datasource = remoteSystemDatasourceService.getDatasource(dataSourceId);
        return Optional.ofNullable(datasource.getContent());
    }

    /**
     * 获取系统用户信息
     * @return x
     */
    public List<SystemUserDto> systemUserList() {
        ResultBean<List<SystemUserDto>> list = remoteSystemUserService.list();
        BmAssetUtils.isTrue(list.isOk(), list.getMsg());
        return list.getContent();
    }
}
